package com.acheli.registry.resource;

import com.acheli.ACHeli;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.PackResources;
import net.minecraft.server.packs.PackType;
import net.minecraft.server.packs.metadata.MetadataSectionSerializer;
import net.minecraft.server.packs.resources.IoSupplier;
import org.jetbrains.annotations.Nullable;

import java.io.*;
import java.nio.file.Files;
import java.util.Set;

public class DynamicResourcePack implements PackResources {
    private static final String MOD_ID = ACHeli.MODID; // 替换为你的模组ID
//    private final String packId;
    private String content = "";
    private final String packRoot;
//    private final String packMcPath;
//
    private final ResourceLocation location;

    private final File file;
    private final ByteArrayInputStream inputStream;


    public DynamicResourcePack(ResourceLocation location, String packRoot, File file, ByteArrayInputStream inputStream) {
//        this.packId = packName;
        this.location = location;
        this.packRoot = packRoot;
        this.file = file;
//        this.packMcPath = packMcPath;
        this.inputStream = inputStream;

        reload();
    }

    // 返回资源包的名称
    @Override
    public String packId() {
        return String.valueOf(location);
    }

    // 定义该资源包管理的命名空间（例如你的模组ID）
    @Override
    public Set<String> getNamespaces(PackType type) {
        return Set.of(MOD_ID);
    }

    // 获取动态资源的输入流（核心方法）
    @Nullable
    @Override
    public IoSupplier<InputStream> getResource(PackType type, ResourceLocation location) {
        // 仅处理客户端资源且属于你的模组命名空间
        if (type == PackType.CLIENT_RESOURCES
//                && location.getNamespace().equals(MOD_ID)
        ) {
//            System.out.println("getResource "+location);
            // 动态模型资源
            if (location.equals(this.location)) {
//                System.out.println("MATCH");
                return () -> inputStream;
            }
        }

        return null;
    }

    // 列出所有动态资源（必须实现）
    @Override
    public void listResources(PackType type, String namespace, String path, ResourceOutput output) {
//        System.out.println("SCAN PATH => "+path);
        if (type == PackType.CLIENT_RESOURCES && path.equals(packRoot)
//                && namespace.equals(MOD_ID)
        ) {
            // 声明动态模型资源
//            System.out.println("MATCH 2");
//            ResourceLocation modelLocation = ResourceLocation.tryBuild(MOD_ID, packRoot+"/"+packMcPath);
            output.accept(
                    location,
                    () -> inputStream // 输入流供应商
            );
        }
    }


    // 其他方法保持默认实现
    @Override
    public @Nullable IoSupplier<InputStream> getRootResource(String... elements) {
        return null;
    }

    @Override
    public @Nullable <T> T getMetadataSection(MetadataSectionSerializer<T> deserializer) {
        return null;
    }

    @Override
    public void close() {}

    public InputStream getInputStream() {
        return inputStream;
    }

    public File getFile() {
        return file;
    }

    public String getContent() {
        return content;
    }

    public void reload() {
        try {
            content = new String(Files.readAllBytes(getFile().toPath()));
        } catch (Exception ignored) {}
    }
}